package com.tbit.uqbike.webmanager.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.RechargeGiveLog;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.value.QueryVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.RechargeGiveLogService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jing
 * @date 2020/11/1013:50
 * @Description:充值赠送日志
 */
@RestController
@RequestMapping("/rechargeGiveLog")
public class RechargeGiveLogController extends BaseController {
    @Autowired
    private RechargeGiveLogService rechargeGiveLogService;

    /**
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description:分类统计赠送
     * @createDate 2020/11/11 14:42
     */
    @RequestMapping("/getByType")
    public JsonResult getByType(RechargeGiveLog rechargeGiveLog) {
        Assert.notNull(rechargeGiveLog.getAccountId(), "ver.accountId.notNull");
        Assert.notNull(rechargeGiveLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(rechargeGiveLog.getEndTime(), "sugar.endRideDt.notNull");
//        List<Integer> moneys = rechargeConfigService.getByAccountId(rechargeGiveLog.getAccountId()).stream().map(RechargeConfig::getMoney).collect(Collectors.toList());
        /**为true即统计赠送金额*/
        if (null == rechargeGiveLog.getFlag()) {
            rechargeGiveLog.setFlag(true);
        }
        startPage();
//        rechargeGiveLog.setPage(new Page(rechargeGiveLog.getPageNO(),rechargeGiveLog.getRowCount()));
//        PageHelperUtil.initPage(rechargeGiveLog.getPage());
        List<Map<String, Object>> maps = rechargeGiveLogService.getByType(new HashMap<String, Object>(), rechargeGiveLog.getAccountId(), rechargeGiveLog.getStartTime(), rechargeGiveLog.getEndTime(), rechargeGiveLog.getFlag(), null, rechargeGiveLog.getPage());
        return JsonResult.ok(maps, (int) new PageInfo<>(maps).getTotal());
    }

    /**
     * @param ob
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 根据条件获取充值赠送记录
     * @createDate 2020/11/11 14:41
     */
    @RequestMapping("/getRechargeGiveLog")
    public JsonResult getRechargeGiveLog(@RequestBody(required = false) QueryVO ob) {
        RechargeGiveLog give = JSON.toJavaObject(ob.getObject(), RechargeGiveLog.class);
        Assert.notNull(give.getAccountId(), "ver.accountId .notNull");
        Page page = ob.getPage();
        PageHelperUtil.initPage(page);
        PageInfo<RechargeGiveLog> pageInfo = rechargeGiveLogService.getRechargeGiveLog(give, page);
        return JsonResult.ok(pageInfo);
    }

    /**
     * @描述 获取充值赠送金额总金额和总人数
     * @参数
     * @返回值
     * @创建人 hzw
     * @创建时间 2021/10/18
     * @修改人和其它信息
     */
    @RequestMapping("/getTotal")
    public JsonResult getTotal(RechargeGiveLog rechargeGiveLog) {
        Assert.notNull(rechargeGiveLog.getAccountId(), "ver.accountId .notNull");
        Assert.notNull(rechargeGiveLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(rechargeGiveLog.getEndTime(), "sugar.endRideDt.notNull");
        Map<String, Object> map = rechargeGiveLogService.getTotal(rechargeGiveLog);
        Double money = Double.parseDouble(map.get("money").toString()) / 100.0;
        return JsonResult.ok(map.get("total"), money);
    }

}
